package ru.zombator.taskman.core.ui;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.layout.GridData;

import ru.zombator.taskman.core.query.Protocol;

public class ChooseProtocolDialog extends Dialog {
	private final String LOGIN_LOGO = "loginLogo";
	private ImageRegistry LOGIN_REGISTRY;
	
	private Protocol protocol = Protocol.RMI;

	public ChooseProtocolDialog(Shell parentShell) {
		super(parentShell);
		LOGIN_REGISTRY = new ImageRegistry();
		LOGIN_REGISTRY.put(LOGIN_LOGO, ImageDescriptor.createFromURL(getClass()
				.getResource("/images/task_small.jpg")));
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		getShell().setText("Выбор протокола");
		
		Composite area = new Composite(parent, SWT.NONE);
		area.setLayout(new GridLayout(2, false));
		
		Label label = new Label(area, SWT.NONE);
		label.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 2, 1));
		label.setImage(LOGIN_REGISTRY.get(LOGIN_LOGO));
		
		Label lblProtocol = new Label(area, SWT.NONE);
		lblProtocol.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblProtocol.setText("Выберите протокол:");
		
		final Combo cboProtocol = new Combo(area, SWT.READ_ONLY);
		cboProtocol.setItems(new String[] {"RMI", "CORBA", "TCP", "UDP"});
		cboProtocol.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		cboProtocol.select(0);
		cboProtocol.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				protocol = Protocol.values()[cboProtocol.getSelectionIndex()];
			}
		});
		
		return area;
	}

	public Protocol getProtocol() {
		return this.protocol;
	}
}
